﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CashOrCard.Models;
using CashOrCard.Helper;

namespace CashOrCard.Controllers
{
    public class PromotionController : Controller
    {
        private CashOrCardContext db = new CashOrCardContext();

        //
        // GET: /Promotion/

        public ActionResult Index()
        {
            return View(db.Promotions.ToList());
        }


        public JsonResult GetPromotionComments(int id = 0)
        {
            var comments = db.Promotions.Find(id).PromotionComments.Where(x=>x.IsEnable.Equals(true)).OrderByDescending(x=>x.CreateDate);
            List<dynamic> list = new List<dynamic>();
            foreach (PromotionComment comment in comments)
            {
                string userName = string.IsNullOrEmpty(comment.User.FirstName) ? comment.User.Email : string.Format("{0},{1}", comment.User.FirstName, comment.User.LastName);
                list.Add(new { commentId = comment.PromotionCommentId, message = comment.PromotionCommentBody, createdate = comment.CreateDate, userName = userName });
            }
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        public JsonResult AddPromotionComments(int promotionId, string comment)
        {
            if (HttpContext.Request.IsAuthenticated)
            {
                PromotionComment commentObj = new PromotionComment();
                commentObj.CreateDate = DateTime.Now;
                commentObj.IsEnable = true;
                commentObj.PromotionCommentBody = comment;
                commentObj.UserId = Common.CurrentUser.UserId;
                db.Promotions.Find(promotionId).PromotionComments.Add(commentObj);


                db.SaveChanges();
                return Json(new { result = "success" });
            }
            else
            {
                return Json(new { result = "NotAuthenticated" });
            }
        }

        [HttpPost]
        public JsonResult AddPromotionFakeComments(int promotionId, string fakeComment)
        {
            if (HttpContext.Request.IsAuthenticated)
            {
                FakeComment commentObj = new FakeComment();
                commentObj.CreateDate = DateTime.Now;
                commentObj.IsEnable = true;
                commentObj.IsClosed = false;                
                commentObj.CommentBody = fakeComment;
                commentObj.UserId = Common.CurrentUser.UserId;
                commentObj.PromotionId = promotionId;
                db.FakeComments.Add(commentObj);                
                db.SaveChanges();
                return Json(new { result = "success" });
            }
            else
            {
                return Json(new { result = "NotAuthenticated" });
            }
        }

        //
        // GET: /Promotion/Details/5

        public ActionResult Details(int id = 0)
        {
            Promotion promotion = db.Promotions.Find(id);
            if (promotion == null)
            {
                return HttpNotFound();
            }
            return View(promotion);
        }

        //
        // GET: /Promotion/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Promotion/Create

        [HttpPost]
        public ActionResult Create(Promotion promotion)
        {
            if (ModelState.IsValid)
            {
                db.Promotions.Add(promotion);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(promotion);
        }

        //
        // GET: /Promotion/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Promotion promotion = db.Promotions.Find(id);
            if (promotion == null)
            {
                return HttpNotFound();
            }
            return View(promotion);
        }

        //
        // POST: /Promotion/Edit/5

        [HttpPost]
        public ActionResult Edit(Promotion promotion)
        {
            if (ModelState.IsValid)
            {
                db.Entry(promotion).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(promotion);
        }

        //
        // GET: /Promotion/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Promotion promotion = db.Promotions.Find(id);
            if (promotion == null)
            {
                return HttpNotFound();
            }
            return View(promotion);
        }

        //
        // POST: /Promotion/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Promotion promotion = db.Promotions.Find(id);
            db.Promotions.Remove(promotion);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}